﻿using System;
using System.Collections.Generic;
using System.Diagnostics;


namespace Framework
{
    /// <summary>
    /// 对象类型检测扩展
    /// </summary>
    public class Check
    {
        internal Check()
        {
        }
        public class Argument
        {
            internal Argument()
            {
            }
            /// <summary>
            /// 检查GUID是否为空
            /// </summary>
            /// <param name="argument"></param>
            /// <param name="argumentName"></param>
            [DebuggerStepThrough]
            public static void IsNotEmpty(Guid argument, string argumentName)
            {
                if (argument == Guid.Empty)
                {
                    throw new ArgumentException("\"{0}\" 是空的GUID.".FormatWith(argumentName));
                }
            }
            /// <summary>
            /// 检查字符串是否为空
            /// </summary>
            /// <param name="argument"></param>
            /// <param name="argumentName"></param>
            [DebuggerStepThrough]
            public static void IsNotEmpty(string argument, string argumentName)
            {
                if (string.IsNullOrEmpty((argument ?? string.Empty).Trim()))
                {
                    throw new ArgumentException("\"{0}\" 不能为空.".FormatWith(argumentName));
                }
            }
            /// <summary>
            /// 检查字符串长度
            /// </summary>
            /// <param name="argument"></param>
            /// <param name="length"></param>
            /// <param name="argumentName"></param>
            [DebuggerStepThrough]
            public static void IsNotOutOfLength(string argument, int length, string argumentName)
            {
                IsNotNull(argument, argumentName);
                if (argument.Trim().Length > length)
                {
                    throw new ArgumentException("\"{0}\" 长度不能超过 {1} 字符.".FormatWith(argumentName, length));
                }
            }

            /// <summary>
            /// 检查字符串长度
            /// </summary>
            /// <param name="argument"></param>
            /// <param name="length"></param>
            /// <param name="isNotEmpty"> </param>
            /// <param name="argumentName"></param>
            [DebuggerStepThrough]
            public static void IsNotOutOfLength(string argument, int length, bool isNotEmpty, string argumentName)
            {
                if (isNotEmpty)
                    IsNotEmpty(argument, argumentName);
                if (argument.Trim().Length > length)
                {
                    throw new ArgumentException("\"{0}\" 长度不能超过 {1} 字符.".FormatWith(argumentName, length));
                }
            }
            /// <summary>
            /// 检查字符串长度必须在指定长度之间
            /// </summary>
            /// <param name="argument"></param>
            /// <param name="minLength"></param>
            /// <param name="maxLength"></param>
            /// <param name="argumentName"></param>
            [DebuggerStepThrough]
            public static void IsNotOutOfLength(string argument, int minLength, int maxLength, string argumentName)
            {
                IsNotNull(argument, argumentName);
                var count = argument.Trim().Length;
                if (count > maxLength || count < minLength)
                {
                    throw new ArgumentException("\"{0}\" 长度必须在{1}-{2}字符之间.".FormatWith(argumentName, minLength, maxLength));
                }
            }
            /// <summary>
            /// 判断对象是否为空
            /// </summary>
            /// <param name="argument">参数</param>
            /// <param name="argumentName">参数名</param>
            [DebuggerStepThrough]
            public static void IsNotNull(object argument, string argumentName)
            {
                if (argument == null)
                {
                    throw new ArgumentNullException(argumentName);
                }
            }

            [DebuggerStepThrough]
            public static void IsNotNegative(int argument, string argumentName)
            {
                if (argument < 0)
                {
                    throw new ArgumentOutOfRangeException(argumentName);
                }
            }

            [DebuggerStepThrough]
            public static void IsNotNegativeOrZero(int argument, string argumentName)
            {
                if (argument <= 0)
                {
                    throw new ArgumentOutOfRangeException(argumentName);
                }
            }

            [DebuggerStepThrough]
            public static void IsNotNegative(long argument, string argumentName)
            {
                if (argument < 0)
                {
                    throw new ArgumentOutOfRangeException(argumentName);
                }
            }

            [DebuggerStepThrough]
            public static void IsNotNegativeOrZero(long argument, string argumentName)
            {
                if (argument <= 0)
                {
                    throw new ArgumentOutOfRangeException(argumentName);
                }
            }

            [DebuggerStepThrough]
            public static void IsNotNegative(float argument, string argumentName)
            {
                if (argument < 0)
                {
                    throw new ArgumentOutOfRangeException(argumentName);
                }
            }

            [DebuggerStepThrough]
            public static void IsNotNegativeOrZero(float argument, string argumentName)
            {
                if (argument <= 0)
                {
                    throw new ArgumentOutOfRangeException(argumentName);
                }
            }

            [DebuggerStepThrough]
            public static void IsNotNegative(decimal argument, string argumentName)
            {
                if (argument < 0)
                {
                    throw new ArgumentOutOfRangeException(argumentName);
                }
            }

            [DebuggerStepThrough]
            public static void IsNotNegativeOrZero(decimal argument, string argumentName)
            {
                if (argument <= 0)
                {
                    throw new ArgumentOutOfRangeException(argumentName);
                }
            }

            [DebuggerStepThrough]
            public static void IsNotInvalidDate(DateTime argument, string argumentName)
            {
                if (!argument.IsValid())
                {
                    throw new ArgumentOutOfRangeException(argumentName);
                }
            }

            [DebuggerStepThrough]
            public static void IsNotInPast(DateTime argument, string argumentName)
            {
                if (argument < SystemTime.Now())
                {
                    throw new ArgumentOutOfRangeException(argumentName);
                }
            }

            [DebuggerStepThrough]
            public static void IsNotInFuture(DateTime argument, string argumentName)
            {
                if (argument > SystemTime.Now())
                {
                    throw new ArgumentOutOfRangeException(argumentName);
                }
            }

            [DebuggerStepThrough]
            public static void IsNotNegative(TimeSpan argument, string argumentName)
            {
                if (argument < TimeSpan.Zero)
                {
                    throw new ArgumentOutOfRangeException(argumentName);
                }
            }

            [DebuggerStepThrough]
            public static void IsNotNegativeOrZero(TimeSpan argument, string argumentName)
            {
                if (argument <= TimeSpan.Zero)
                {
                    throw new ArgumentOutOfRangeException(argumentName);
                }
            }

            [DebuggerStepThrough]
            public static void IsNotEmpty<T>(ICollection<T> argument, string argumentName)
            {
                IsNotNull(argument, argumentName);

                if (argument.Count == 0)
                {
                    throw new ArgumentException("集合不能为空.");
                }
            }

            [DebuggerStepThrough]
            public static void IsNotOutOfRange(int argument, int min, int max, string argumentName)
            {
                if ((argument < min) || (argument > max))
                {
                    throw new ArgumentOutOfRangeException(argumentName, "{0} 必须在 \"{1}\"-\"{2}\"之间.".FormatWith(argumentName, min, max));
                }
            }
            [DebuggerStepThrough]
            public static void IsNotInvalidQq(string argument, string argumentName)
            {
                IsNotEmpty(argument, argumentName);
                if (!argument.IsQq())
                {
                    throw new ArgumentException("不是有效QQ号码.".FormatWith(argumentName), argumentName);
                }
            }

            [DebuggerStepThrough]
            public static void IsNotInvalidQq(string argument, bool isAllowNull, string argumentName)
            {
                if (isAllowNull && argument.IsNullOrEmpty())
                {
                    return;
                }
                IsNotEmpty(argument, argumentName);
                if (!argument.IsQq())
                {
                    throw new ArgumentException("不是有效的不是有效地址.".FormatWith(argumentName));
                }
            }
            [DebuggerStepThrough]
            public static void IsNotInvalidEmail(string argument, string argumentName)
            {
                IsNotEmpty(argument, argumentName);
                if (!argument.IsEmail())
                {
                    throw new ArgumentException("不是有效的Email地址.".FormatWith(argumentName));
                }
            }

            [DebuggerStepThrough]
            public static void IsSafeSql(string argument, string argumentName)
            {
                IsNotEmpty(argument, argumentName);
                if (!argument.IsSqlSafe())
                {
                    throw new ArgumentException("不是有效的SQL语句.".FormatWith(argumentName));
                }
            }

            [DebuggerStepThrough]
            public static void IsNotInvalidEmail(string argument, bool isAllowNull, string argumentName)
            {
                if (isAllowNull && argument.IsNullOrEmpty())
                {
                    return;
                }
                IsNotEmpty(argument, argumentName);
                if (!argument.IsEmail())
                {
                    throw new ArgumentException("不是有效的Email地址.".FormatWith(argumentName));
                }
            }

            [DebuggerStepThrough]
            public static void IsNotInvalidWebUrl(string argument, string argumentName)
            {
                IsNotEmpty(argument, argumentName);
                if (!argument.IsWebUrl())
                {
                    throw new ArgumentException("不是有效Url地址.".FormatWith(argumentName), argumentName);
                }
            }
            [DebuggerStepThrough]
            public static void IsNotInvalidWebUrl(string argument, bool isAllowNull, string argumentName)
            {
                if (isAllowNull && argument.IsNullOrEmpty())
                {
                    return;
                }
                IsNotEmpty(argument, argumentName);
                if (!argument.IsWebUrl())
                {
                    throw new ArgumentException("不是有效Url地址.".FormatWith(argumentName));
                }
            }
            /// <summary>
            /// 手机号码验证
            /// </summary>
            /// <param name="argument"></param>
            /// <param name="argumentName"></param>
            [DebuggerStepThrough]
            public static void IsNotInvalidMobilePhoneNumber(string argument, string argumentName)
            {
                IsNotEmpty(argument, argumentName);
                if (!argument.IsMobilePhoneNumber())
                {
                    throw new ArgumentException("不是有效的手机号码.".FormatWith(argumentName));
                }
            }

            /// <summary>
            /// 手机号码验证
            /// </summary>
            /// <param name="argument"></param>
            /// <param name="isAllowNull"> </param>
            /// <param name="argumentName"></param>
            [DebuggerStepThrough]
            public static void IsNotInvalidMobilePhoneNumber(string argument, bool isAllowNull, string argumentName)
            {
                if (isAllowNull && argument.IsNullOrEmpty())
                {
                    return;
                }
                IsNotEmpty(argument, argumentName);
                if (!argument.IsMobilePhoneNumber())
                {
                    throw new ArgumentException("不是有效的手机号码.".FormatWith(argumentName));
                }
            }
            /// <summary>
            /// 电话号码验证
            /// </summary>
            /// <param name="argument"></param>
            /// <param name="argumentName"></param>
            [DebuggerStepThrough]
            public static void IsNotInvalidTelephoneNumber(string argument, string argumentName)
            {
                IsNotEmpty(argument, argumentName);
                if (!argument.IsTelephoneNumber())
                {
                    throw new ArgumentException("不是有效的电话号码.".FormatWith(argumentName));
                }
            }

            /// <summary>
            /// 电话号码验证
            /// </summary>
            /// <param name="argument"></param>
            /// <param name="isAllowNull"> </param>
            /// <param name="argumentName"></param>
            [DebuggerStepThrough]
            public static void IsNotInvalidTelephoneNumber(string argument, bool isAllowNull, string argumentName)
            {
                if (isAllowNull && argument.IsNullOrEmpty())
                {
                    return;
                }
                IsNotEmpty(argument, argumentName);
                if (!argument.IsTelephoneNumber())
                {
                    throw new ArgumentException("不是有效的电话号码.".FormatWith(argumentName));
                }
            }
            /// <summary>
            /// 是否是身份证号码
            /// </summary>
            /// <param name="argument"></param>
            /// <param name="argumentName"></param>
            [DebuggerStepThrough]
            public static void IsNotInvalidIdentityCard(string argument, string argumentName)
            {
                IsNotEmpty(argument, argumentName);
                if (!argument.IsIdentityCard())
                {
                    throw new ArgumentException("不是有效的身份证号码.".FormatWith(argumentName));
                }
            }

            /// <summary>
            /// 是否是身份证号码
            /// </summary>
            /// <param name="argument"></param>
            /// <param name="isAllowNull"> </param>
            /// <param name="argumentName"></param>
            [DebuggerStepThrough]
            public static void IsNotInvalidIdentityCard(string argument, bool isAllowNull, string argumentName)
            {
                if (isAllowNull && argument.IsNullOrEmpty())
                {
                    return;
                }
                IsNotEmpty(argument, argumentName);
                if (!argument.IsIdentityCard())
                {
                    throw new ArgumentException("不是有效的身份证号码.".FormatWith(argumentName));
                }
            }
            /// <summary>
            /// 检查数据库有效时间
            /// </summary>
            /// <param name="argument"></param>
            /// <param name="argumentName"></param>
            [DebuggerStepThrough]
            public static void IsNotInvalidSqlDateTime(DateTime argument, string argumentName)
            {
                if (argument < System.Data.SqlTypes.SqlDateTime.MinValue.Value || argument > System.Data.SqlTypes.SqlDateTime.MaxValue)
                {
                    throw new ArgumentException("\"{0}\" 不是在有效的数据日期范围内.".FormatWith(argumentName));
                }

            }




        }

    }
}